import {Category} from "../../models/categroy"
import {User} from "../../models/user"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        Form: {
            nickname: '',
            qq_number: '',
            mobile: '',
            alipay_name: '',
            alipay_account: '',
            speciality: '',
            description: ''
        },
        items: [],
        tipType: 'text',
        isSubmitValidate: 1,
        alignType: 'start',
        nicknameRules: [
            {required: true, message: '请输入昵称', trigger: 'blur'},
            {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'},
        ],
        qqRules: [
            {required: true, message: '请输入正确的qq号', trigger: 'blur', pattern: '/^[1-9][0-9]{4,15}$/'},
        ],
        alipayaccountRules: [
            {
                required: true,
                message: "支付宝帐号必填!",
                trigger: 'blur'
            },
            {
                pattern:'/^(?:\\w+\\.?)*\\w+@(?:\\w+\\.)+\\w+|\\d{9,11}$/',
                message:'支付宝账号格式不正确!',
                trigger: 'blur'
            }
        ],
        alipaynameRules: [
            {
                required: true,
                message: "请填写支付宝姓名!",
                trigger: 'blur'
            },
            {
                pattern:'/^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/',
                message: "请输入正确的姓名!",
                trigger: 'blur'
            }
        ],
        descRules: {min: 10, max: 140, message: '请输入10到140个字的自我描述', trigger: 'change'},
        itemsRules: [
            {type: 'array', required: true, message: '类目必选', trigger: 'change'},
        ],

    },

    change(e) {
        let items = this.data.items;
        items.forEach(item => {
            if (item.id.toString() === e.detail.key) {
                item.checked = e.detail.checked;
            }
        });
        this.setData({
            items: items
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function () {
        wx.lin.initValidateForm(this);
        const categories = await Category.getCategories()
        this.setData({
            items: categories
        })
    },

    async submit(event) {
        const {detail} = event;
        if (!detail.isValidate) {
            return
        }
        const data = detail.values
        let specialityList = []
        detail.values.speciality.forEach(item => {
            specialityList.push(item.value);
        })

        data.speciality = specialityList.join(',');
        const res = await User.registerUserInfo(data);
        if (res.code === 0) {
            wx.showToast({
                icon: "none",
                title: "入驻成功",
                duration: 3000
            })
            setTimeout(function () {
                wx.navigateBack({
                    delta: 1
                })
            }, 3000) //延迟时间 这里是1秒

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
});
